package com.frank.oj.service.entity.msg.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frank.model.entity.contest.Contest;
import com.frank.model.entity.discussion.Discussion;
import com.frank.model.entity.discussion.Reply;
import com.frank.model.entity.msg.MsgRemind;
import com.frank.oj.constant.Constants;
import com.frank.oj.mapper.ReplyMapper;
import com.frank.oj.model.vo.ReplyVO;
import com.frank.oj.service.entity.contest.ContestEntityService;
import com.frank.oj.service.entity.discussion.DiscussionEntityService;
import com.frank.oj.service.entity.msg.MsgRemindEntityService;
import com.frank.oj.service.entity.msg.ReplyEntityService;
import com.frank.oj.service.entity.user.UserInfoEntityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author frank
 * @Date 2024/4/17
 */
@Service
public class ReplyEntityServiceImpl extends ServiceImpl<ReplyMapper, Reply> implements ReplyEntityService {
    @Resource
    private ContestEntityService contestEntityService;
    @Resource
    private UserInfoEntityService userInfoEntityService;
    @Resource
    private ReplyMapper replyMapper;
    @Resource
    private DiscussionEntityService discussionEntityService;
    @Resource
    private MsgRemindEntityService msgRemindEntityService;

    @Override
    public List<ReplyVO> getAllReplyByCommentId(Long cid, String uid, boolean isRoot, Integer commentId) {
        if (cid != null) { //比赛
            Contest contest = contestEntityService.getById(cid);
            boolean onlyMineAndAdmin = contest.getStatus().equals(Constants.Contest.STATUS_RUNNING.getCode()) && !isRoot && !contest.getUid().equals(uid);

            if (onlyMineAndAdmin) { //进行中的比赛仅自己和管理员能看
                List<String> superAdminUidList = userInfoEntityService.getSuperAdminUidList();
                superAdminUidList.add(uid);
                superAdminUidList.add(contest.getUid());

                return replyMapper.getAllReplyByCommentId(commentId, superAdminUidList);
            }
        }
        return replyMapper.getAllReplyByCommentId(commentId, null);
    }

    @Override
    public void updateReplyMsg(Integer sourceId, String sourceType, String content, Integer quoteId, String quoteType, String recipientId, String senderId) {
        if (content.length() > 200) {
            content = content.substring(0, 200) + "...";
        }
        MsgRemind msgRemind = new MsgRemind();
        msgRemind.setAction("Reply")
                .setSourceId(sourceId)
                .setSourceType(sourceType)
                .setSourceContent(content)
                .setQuoteId(quoteId)
                .setQuoteType(quoteType)
                .setRecipientId(recipientId)
                .setSenderId(senderId);

        messageAlert(sourceId, sourceType, msgRemind, discussionEntityService, msgRemindEntityService);
    }

    /**
     * 信息提醒，含原消息地址
     *
     * @param sourceId   评论id
     * @param sourceType 类型 ：Discussion/Contest
     * @param msgRemind  信息主体
     */
    public static void messageAlert(Integer sourceId, String sourceType, MsgRemind msgRemind,
                                    DiscussionEntityService discussionEntityService,
                                    MsgRemindEntityService msgRemindEntityService) {
        if ("Discussion".equals(sourceType)) {
            Discussion discussion = discussionEntityService.getById(sourceId);
            if (discussion != null) {
                if (discussion.getGid() != null) {
                    msgRemind.setUrl("/group/" + discussion.getGid() + "/discussion-detail/" + sourceId);
                } else {
                    msgRemind.setUrl("/discussion-detail/" + sourceId);
                }
            }
        } else if (sourceType.equals("Contest")) {
            msgRemind.setUrl("/contest/" + sourceId + "/comment");
        }

        msgRemindEntityService.saveOrUpdate(msgRemind);
    }
}
